<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>range.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_dom_range.js.html">range.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2007 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Utilities for working with ranges in HTML documents.
<a name="line17"></a> *
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.dom.Range&#39;);
<a name="line21"></a>
<a name="line22"></a>goog.require(&#39;goog.dom&#39;);
<a name="line23"></a>goog.require(&#39;goog.dom.AbstractRange&#39;);
<a name="line24"></a>goog.require(&#39;goog.dom.ControlRange&#39;);
<a name="line25"></a>goog.require(&#39;goog.dom.MultiRange&#39;);
<a name="line26"></a>goog.require(&#39;goog.dom.NodeType&#39;);
<a name="line27"></a>goog.require(&#39;goog.dom.TextRange&#39;);
<a name="line28"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>/**
<a name="line32"></a> * Create a new selection from the given browser window&#39;s current selection.
<a name="line33"></a> * Note that this object does not auto-update if the user changes their
<a name="line34"></a> * selection and should be used as a snapshot.
<a name="line35"></a> * @param {Window} opt_win The window to get the selection of.  Defaults to the
<a name="line36"></a> *     window this class was defined in.
<a name="line37"></a> * @return {goog.dom.AbstractRange?} A range wrapper object, or null if there
<a name="line38"></a> *     was an error.
<a name="line39"></a> */
<a name="line40"></a>goog.dom.Range.createFromWindow = function(opt_win) {
<a name="line41"></a>  var sel = goog.dom.AbstractRange.getBrowserSelectionForWindow(
<a name="line42"></a>      opt_win || window);
<a name="line43"></a>  return sel &amp;&amp; goog.dom.Range.createFromBrowserSelection(sel);
<a name="line44"></a>};
<a name="line45"></a>
<a name="line46"></a>
<a name="line47"></a>/**
<a name="line48"></a> * Create a new range wrapper from the given browser selection object.  Note
<a name="line49"></a> * that this object does not auto-update if the user changes their selection and
<a name="line50"></a> * should be used as a snapshot.
<a name="line51"></a> * @param {!Object} selection The browser selection object.
<a name="line52"></a> * @return {goog.dom.AbstractRange?} A range wrapper object or null if there
<a name="line53"></a> *    was an error.
<a name="line54"></a> */
<a name="line55"></a>goog.dom.Range.createFromBrowserSelection = function(selection) {
<a name="line56"></a>  var range;
<a name="line57"></a>  var isReversed = false;
<a name="line58"></a>  if (selection.createRange) {
<a name="line59"></a>    /** @preserveTry */
<a name="line60"></a>    try {
<a name="line61"></a>      range = selection.createRange();
<a name="line62"></a>    } catch (e) {
<a name="line63"></a>      // Access denied errors can be thrown here in IE if the selection was
<a name="line64"></a>      // a flash obj or if there are cross domain issues
<a name="line65"></a>      return null;
<a name="line66"></a>    }
<a name="line67"></a>  } else if (selection.rangeCount) {
<a name="line68"></a>    if (selection.rangeCount &gt; 1) {
<a name="line69"></a>      return goog.dom.MultiRange.createFromBrowserSelection(
<a name="line70"></a>          /** @type {Selection} */ (selection));
<a name="line71"></a>    } else {
<a name="line72"></a>      range = selection.getRangeAt(0);
<a name="line73"></a>      isReversed = goog.dom.Range.isReversed(selection.anchorNode,
<a name="line74"></a>          selection.anchorOffset, selection.focusNode, selection.focusOffset);
<a name="line75"></a>    }
<a name="line76"></a>  } else {
<a name="line77"></a>    return null;
<a name="line78"></a>  }
<a name="line79"></a>
<a name="line80"></a>  return goog.dom.Range.createFromBrowserRange(range, isReversed);
<a name="line81"></a>};
<a name="line82"></a>
<a name="line83"></a>
<a name="line84"></a>/**
<a name="line85"></a> * Create a new range wrapper from the given browser range object.
<a name="line86"></a> * @param {Range|TextRange} range The browser range object.
<a name="line87"></a> * @param {boolean} opt_isReversed Whether the focus node is before the anchor
<a name="line88"></a> *     node.
<a name="line89"></a> * @return {goog.dom.AbstractRange} A range wrapper object.
<a name="line90"></a> */
<a name="line91"></a>goog.dom.Range.createFromBrowserRange = function(range, opt_isReversed) {
<a name="line92"></a>  // Create an IE control range when appropriate.
<a name="line93"></a>  return goog.dom.AbstractRange.isNativeControlRange(range) ?
<a name="line94"></a>      goog.dom.ControlRange.createFromBrowserRange(range) :
<a name="line95"></a>      goog.dom.TextRange.createFromBrowserRange(range, opt_isReversed);
<a name="line96"></a>};
<a name="line97"></a>
<a name="line98"></a>
<a name="line99"></a>/**
<a name="line100"></a> * Create a new range wrapper that selects the given node&#39;s text.
<a name="line101"></a> * @param {Node} node The node to select.
<a name="line102"></a> * @param {boolean} opt_isReversed Whether the focus node is before the anchor
<a name="line103"></a> *     node.
<a name="line104"></a> * @return {goog.dom.AbstractRange} A range wrapper object.
<a name="line105"></a> */
<a name="line106"></a>goog.dom.Range.createFromNodeContents = function(node, opt_isReversed) {
<a name="line107"></a>  // TODO: Use control ranges for IMGs and TDs?
<a name="line108"></a>  return goog.dom.TextRange.createFromNodeContents(node, opt_isReversed);
<a name="line109"></a>};
<a name="line110"></a>
<a name="line111"></a>
<a name="line112"></a>/**
<a name="line113"></a> * Create a new range wrapper that represents a caret at the given node,
<a name="line114"></a> * accounting for the given offset.  This always creates a TextRange, regardless
<a name="line115"></a> * of whether node is an image node or other control range type node.
<a name="line116"></a> * @param {Node} node The node to place a caret at.
<a name="line117"></a> * @param {number} offset The offset within the node to place the caret at.
<a name="line118"></a> * @return {goog.dom.AbstractRange} A range wrapper object.
<a name="line119"></a> */
<a name="line120"></a>goog.dom.Range.createCaret = function(node, offset) {
<a name="line121"></a>  return goog.dom.TextRange.createFromNodes(node, offset, node, offset);
<a name="line122"></a>};
<a name="line123"></a>
<a name="line124"></a>
<a name="line125"></a>/**
<a name="line126"></a> * Create a new range wrapper that selects the area between the given nodes,
<a name="line127"></a> * accounting for the given offsets.
<a name="line128"></a> * @param {Node} startNode The node to start with.
<a name="line129"></a> * @param {number} startOffset The offset within the node to start.
<a name="line130"></a> * @param {Node} endNode The node to end with.
<a name="line131"></a> * @param {number} endOffset The offset within the node to end.
<a name="line132"></a> * @return {goog.dom.AbstractRange} A range wrapper object.
<a name="line133"></a> */
<a name="line134"></a>goog.dom.Range.createFromNodes = function(startNode, startOffset, endNode,
<a name="line135"></a>    endOffset) {
<a name="line136"></a>  return goog.dom.TextRange.createFromNodes(startNode, startOffset, endNode,
<a name="line137"></a>      endOffset);
<a name="line138"></a>};
<a name="line139"></a>
<a name="line140"></a>
<a name="line141"></a>/**
<a name="line142"></a> * Clears the window&#39;s selection.
<a name="line143"></a> * @param {Window} opt_win The window to get the selection of.  Defaults to the
<a name="line144"></a> *     window this class was defined in.
<a name="line145"></a> */
<a name="line146"></a>goog.dom.Range.clearSelection = function(opt_win) {
<a name="line147"></a>  var sel = goog.dom.AbstractRange.getBrowserSelectionForWindow(
<a name="line148"></a>      opt_win || window);
<a name="line149"></a>  if (!sel) {
<a name="line150"></a>    return;
<a name="line151"></a>  }
<a name="line152"></a>  if (sel.empty) {
<a name="line153"></a>    // We can&#39;t just check that the selection is empty, becuase IE
<a name="line154"></a>    // sometimes gets confused.
<a name="line155"></a>    try {
<a name="line156"></a>      sel.empty();
<a name="line157"></a>    } catch (e) {
<a name="line158"></a>      // Emptying an already empty selection throws an exception in IE
<a name="line159"></a>    }
<a name="line160"></a>  } else {
<a name="line161"></a>    sel.removeAllRanges();
<a name="line162"></a>  }
<a name="line163"></a>};
<a name="line164"></a>
<a name="line165"></a>
<a name="line166"></a>/**
<a name="line167"></a> * Tests if the window has a selection.
<a name="line168"></a> * @param {Window} opt_win The window to check the selection of.  Defaults to
<a name="line169"></a> *     the window this class was defined in.
<a name="line170"></a> * @return {boolean} Whether the window has a selection.
<a name="line171"></a> */
<a name="line172"></a>goog.dom.Range.hasSelection = function(opt_win) {
<a name="line173"></a>  var sel = goog.dom.AbstractRange.getBrowserSelectionForWindow(
<a name="line174"></a>      opt_win || window);
<a name="line175"></a>  return !!sel &amp;&amp; (goog.userAgent.IE ? sel.type != &#39;None&#39; : !!sel.rangeCount);
<a name="line176"></a>};
<a name="line177"></a>
<a name="line178"></a>
<a name="line179"></a>/**
<a name="line180"></a> * Returns whether the focus position occurs before the anchor position.
<a name="line181"></a> * @param {Node} anchorNode The node to start with.
<a name="line182"></a> * @param {number} anchorOffset The offset within the node to start.
<a name="line183"></a> * @param {Node} focusNode The node to end with.
<a name="line184"></a> * @param {number} focusOffset The offset within the node to end.
<a name="line185"></a> * @return {boolean} Whether the focus position occurs before the anchor
<a name="line186"></a> *     position.
<a name="line187"></a> */
<a name="line188"></a>goog.dom.Range.isReversed = function(anchorNode, anchorOffset, focusNode,
<a name="line189"></a>    focusOffset) {
<a name="line190"></a>  if (anchorNode == focusNode) {
<a name="line191"></a>    return focusOffset &lt; anchorOffset;
<a name="line192"></a>  }
<a name="line193"></a>  var child;
<a name="line194"></a>  if (anchorNode.nodeType == goog.dom.NodeType.ELEMENT &amp;&amp; anchorOffset) {
<a name="line195"></a>    child = anchorNode.childNodes[anchorOffset];
<a name="line196"></a>    if (child) {
<a name="line197"></a>      anchorNode = child;
<a name="line198"></a>      anchorOffset = 0;
<a name="line199"></a>    } else if (goog.dom.contains(anchorNode, focusNode)) {
<a name="line200"></a>      // If focus node is contained in anchorNode, it must be before the
<a name="line201"></a>      // end of the node.  Hence we are reversed.
<a name="line202"></a>      return true;
<a name="line203"></a>    }
<a name="line204"></a>  }
<a name="line205"></a>  if (focusNode.nodeType == goog.dom.NodeType.ELEMENT &amp;&amp; focusOffset) {
<a name="line206"></a>    child = focusNode.childNodes[focusOffset];
<a name="line207"></a>    if (child) {
<a name="line208"></a>      focusNode = child;
<a name="line209"></a>      focusOffset = 0;
<a name="line210"></a>    } else if (goog.dom.contains(focusNode, anchorNode)) {
<a name="line211"></a>      // If anchor node is contained in focusNode, it must be before the
<a name="line212"></a>      // end of the node.  Hence we are not reversed.
<a name="line213"></a>      return false;
<a name="line214"></a>    }
<a name="line215"></a>  }
<a name="line216"></a>  return (goog.dom.compareNodeOrder(anchorNode, focusNode) ||
<a name="line217"></a>      anchorOffset - focusOffset) &gt; 0;
<a name="line218"></a>};
</pre>


</body>
</html>
